package m.jclecai.com.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import m.jclecai.com.R;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.dialog.CPDialog;
import m.jclecai.com.dialog.DialogUtils;
import m.jclecai.com.dialog.LoadingDialog;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.ActivityStack;
import m.jclecai.com.view.XLoading;

/**
 * Created by hp on 2017/3/14.
 */

public abstract class ISuperBaseActivity extends AppCompatActivity implements XLoading.OnRetryClickListener {

    protected DialogUtils mWaitingDialog;
    LoadingDialog dialog;
    protected Callback callback;
    //接口必须参数 作为公共key 避免单独写出现错误 如有其他 直接在下面叠加
    protected static String PAY_BNUM = "bnum";//Yes	string	认购份数
    protected static String PAY_PNUM = "pnum";//Yes	string	保底份数
    protected static String PAY_TYPE = "type";//Yes	string	彩种英文名称
    protected static String PAY_TYPE_NUM = "typeNum";//Yes	string	彩种英文名称
    protected static String PAY_DESC = "desc";//Yes	string	合买宣传描述
    protected static String PAY_CODES = "codes";//	Yes	string	投注号码
    protected static String PAY_GID = "gid";//Yes	string	彩种
    protected static String PAY_HBID = "hbid";//Yes	string	红包ID
    protected static String PAY_HBMONEY = "hbmoney";//	Yes	string	红包金额
    protected static String PAY_MONEY = "money";//	Yes	double	金额
    protected static String PAY_MULTIPLE = "multiple";//	Yes	string	倍数
    protected static String PAY_MULTIPLE_ALL = "multiple_all";//	Yes	string	倍数
    protected static String PAY_NAME = "name";//Yes	string	合买口号、标题
    protected static String PAY_NUM = "num";//Yes	string	追号期数
    protected static String PAY_TERM = "term";//Yes	string	期号
    protected static String PAY_USER = "user";//Yes	string	用户名
    protected static String PAY_PASSWORD = "password";//Yes	string	密码
    protected static String PAY_VOTENUMS = "voteNums";//Yes	注
    protected static String PAY_PASS = "pass";//Yes	过关方式
    protected static String PAY_LOTTERY_TYPE = "lotteryType";//彩票类型 胜平负
    protected static String PAY_LOTTERY_ADDTIONAL = "addtional";//是否追加
    protected static String PAY_LOTTERY_BALLS = "balls";//是否走balls接口购买


    protected static String PAY_WRATE = "wrate";//	Yes	string	总份数
    protected static String PAY_PROJECETYPE = "projectType";//1合买 0自购
    protected static String PAY_OFLAG = "oflag";//彩票类型 胜平负
    protected static String PAY_AUTO_CANCEL = "autocancel";//中奖停止

    //这里我们对于足彩，我们有更的选项了
    protected static String SUN_ORDER_MODE="fflag"; //是否是公开；
    protected static String SUN_ORDER_SLOGAN="fname";   //跟单宣言
    protected static String IS_SHOW_ORDER="followid";   //是否显示跟单


    XLoading mLoadingView;

    //由子类去实现并添加布局
    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract boolean hideCoustomStatusBar();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStack.getInstace().addActivity(this);
    }

    public void showWaitingDialog(int resid) {
        if (mWaitingDialog == null) {
            mWaitingDialog = new DialogUtils(this);
        }
        mWaitingDialog.show();

        mWaitingDialog.title.setText(resid);
    }

    public void showWaitingDialog() {
        if (mWaitingDialog == null) {
            mWaitingDialog = new DialogUtils(this);
        }

        mWaitingDialog.show();
        mWaitingDialog.title.setVisibility(View.GONE);
    }

    public void dissWaitingDialog() {
        if (mWaitingDialog != null) {
            mWaitingDialog.dismiss();
        }
    }



    public void showExceptButton(boolean bool){
        if (mLoadingView != null)
            mLoadingView.showExceptButton(bool);
    }

    public void showDialog() {
        if (isFinishing()) return;
        if (dialog == null) {
            dialog = new LoadingDialog(this);
        }
        dialog.show();
    }

    public void dissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWaitingDialog != null) {
            mWaitingDialog.dismiss();
        }
    }

    protected final void onFinish() {
        hideInputMethod();
        this.finish();
    }

    protected final void hideInputMethod() {
        InputMethodManager inputMethodManager = (InputMethodManager) this
                .getSystemService(INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && this.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 提示的Toast
     *
     * @param content 提示的内容
     */
    public void DisPlay(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (Constant_Result.CLOSEPAGE == requestCode && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        } else if (requestCode == Constant_Result.NEEDLOGIN) {
            if (resultCode == Activity.RESULT_OK) {
                if (callback != null)
                    callback.callbackSuccess();
            } else {
                if (callback != null)
                    callback.callbackField();
            }
        }
    }

    //右边按钮的文本 drawable -1 代表不加图案
    protected abstract void rightOptionText(String text, int drawable);

    //标题文本
    protected abstract void setTitle(String text, int drawable);

    //左边按钮的文本
    protected abstract void leftOptionText(String text, int drawable);

    protected void showAlertDialog(String title, String message, String positionText, String negativeText
            , View.OnClickListener position, View.OnClickListener negative) {
        CPDialog dialog = new CPDialog(this);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setPositiveButton(positionText, position);
        dialog.setNegativeButton(negativeText, negative);
        dialog.show();
    }

    //背景色
    protected abstract int getStatusBarColor();


    public void showLoadingLayout(ViewGroup viewGroup) {
        showLoadingLayout(viewGroup, null);
    }

    public void showLoadingLayout(ViewGroup viewGroup, int resId) {
        showLoadingLayout(viewGroup, setString(resId));
    }

    public void showLoadingLayout(ViewGroup viewGroup, String message) {
        initLoadingLayout(viewGroup);
        mLoadingView.setLoadingMessage(message);
        mLoadingView.showLoadingLayout();
    }

    public void initLoadingLayout(ViewGroup viewGroup) {
        if (mLoadingView == null) {
            if (viewGroup == null)
                return;
            mLoadingView = new XLoading(this, viewGroup);
        }
    }

    public void showInternetOffLayout() {
        showInternetOffLayout(null, null);
    }

    public void showInternetOffLayout(String message, String btnMessage) {
        if (mLoadingView != null) {
            mLoadingView.setOnRetryListener(this);
            mLoadingView.setInternetOffButtonMessage(btnMessage);
            mLoadingView.setInternetOffMessage(message);
            mLoadingView.showInternetOffLayout(btnMessage);
        }
    }

    public void showEmptyLayout() {
        if (mLoadingView != null)
            mLoadingView.setExceptionIcon(R.mipmap.ic_empty);
        showExceptionLayout(setString(R.string.custom_empty_text), null);
    }

    public void showEmptyLayout(String msg, int resId) {
        if (mLoadingView != null)
            mLoadingView.setExceptionIcon(resId);
        showExceptionLayout(msg, null);
    }

    public void showExceptionLayout() {
        if (mLoadingView != null)
            mLoadingView.setExceptionIcon(R.mipmap.ic_warning);
        showExceptionLayout(setString(R.string.cp_api_failed), null);
    }

    public void showExceptionLayout(String message, String btnMessage) {
        if (mLoadingView != null) {
            mLoadingView.setOnRetryListener(this);
            mLoadingView.setOtherExceptionButtonMessage(btnMessage);
            mLoadingView.setOtherExceptionMessage(message);
            mLoadingView.showExceptionLayout(btnMessage);
        }
    }

    public void closeLoadingLayout() {
        if (mLoadingView != null)
            mLoadingView.hideAll();
    }

    //出现数据加载失败异常之后的重试
    @Override
    public void OnRetry(String btnMessage) {

    }

    public String setString(int resId){
        try {
            if(isFinishing())
                return "";
            return getString(resId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
